//
//  HXBtnSelectedView.m
//  KD
//
//  Created by ifly on 16/3/1.
//  Copyright © 2016年 ifly. All rights reserved.
//

#import "HXBtnSelectedView.h"


@interface HXBtnSelectedView()
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) NSMutableArray<UIButton *> *btnMArray;
@property(nonatomic,strong) NSMutableArray<HXSelectedDataDictModel *> *dataMArray;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong) NSMutableArray<HXSelectedDataDictModel *> *nameCodeArray;
@end

@implementation HXBtnSelectedView

+(instancetype)initWithName:(NSString*)name{
    HXBtnSelectedView *btnSelectedView = [[HXBtnSelectedView alloc]init];
    btnSelectedView.name = name;
    return btnSelectedView;
}

//返回本视图所有的code
- (NSString *)allCode{
    //    NSMutableDictionary *mDict = [[NSMutableDictionary alloc]init];
    //    NSString *str = [[NSString alloc]init];
    //    str = @"";
    //    for (NSString *areaStr in personCondition.areaMArray) {
    //        str = [str stringByAppendingString:[areaStr stringByAppendingString:@","]];
    //    }
    //    if (str.length>0)str = [str substringToIndex:(str.length - 1)];
    //    [mDict setObject:str forKey:@"AeraCode"];
    //    str = @"";
    NSString *str = [[NSString alloc]init];
    NSString *namestr = [[NSString alloc]init];
    if ([self.name isEqualToString:@"资质等级"]) {//这时候拼串不需要加逗号
        for (HXSelectedDataDictModel *data in self.nameCodeArray) {
            str = [str stringByAppendingString:data.code];
            namestr = [namestr stringByAppendingString:data.name];
//            HXLog(@"%@,##########",data.name);
        }
    }else{
        for (HXSelectedDataDictModel *data in self.nameCodeArray) {
            str = [str stringByAppendingString:[NSString stringWithFormat:@"%@,",data.code]];
            namestr = [namestr stringByAppendingString:data.name];
//            HXLog(@"%@,##########",data.name);
        }
        if (str.length>0)str = [str substringToIndex:(str.length - 1)];
    }
    HXLog(@"%@,~~~~~~~~%@",namestr,str);
    return str;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        //添加按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn addTarget:self action:@selector(addMsgBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
        [btn setTitle:@"+添加" forState:UIControlStateNormal];
        [self.btnMArray addObject:btn];
    }
    return self;
}

-(void)addMsgBtn:(UIButton*)btn{
    [self.delegate addMsgWithName:self.name];
}

//控制器通知我添加一个按钮
//这里加一个数据 加一个按钮
-(void)giveMeMsg:(NSString*)msg{
    if (msg.length == 0) return;
    NSArray *array = [msg componentsSeparatedByString:@"*"];
    if (array.count==2) {
        HXSelectedDataDictModel *model = [HXSelectedDataDictModel initWithName:array[0] Code:array[1]];
        [self.nameCodeArray addObject:model];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(deleteMeMsg:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:MAINCOLOR forState:UIControlStateNormal];
        // [btn setImage:[UIImage imageNamed:@"BtnX"] forState:UIControlStateNormal];
        [self.btnMArray insertObject:btn atIndex:1];
        [self setNeedsLayout];
    }
    else{
        //.NSAssert(0, @"分割字符串的时候出错");
    }
}

//这里删一个数据 删一个按钮  并且通知控制器刷新资质等级和企业列表
-(void)deleteMeMsg:(UIButton*)btn{
    NSString *str = btn.titleLabel.text;
    for (UIButton*button in self.btnMArray) {
        if ([str isEqualToString:button.titleLabel.text]) {
            [self.btnMArray removeObject:button];
            [button removeFromSuperview];
            break;
        }
    }
    //删一个btn同时删一个数据
    for (HXSelectedDataDictModel *data in self.nameCodeArray) {
        if ([str isEqualToString:data.name]) {
            [self.nameCodeArray removeObject:data];
            break;
        }
    }
    [self setNeedsLayout];
    if ([self.name isEqualToString:@"企业类型"]) {
        if ([self.delegate respondsToSelector:@selector(reloadGradePerson)]) {
            [self.delegate reloadGradePerson];
        }
    }
}

//我自己的高度是40
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat y = 3;
    CGFloat gap = 6.0;
    CGFloat x = 0.0;
    CGFloat height = 34.0;
    for (UIButton *btn in self.btnMArray) {
        CGSize size = [self SizeWithString:btn.titleLabel.text font:[UIFont systemFontOfSize:15]];
        [btn setFrame:CGRectMake(x, y, size.width+11.0, height)];
        x = x + size.width+11 + gap;
        [self.scrollView addSubview:btn];
        btn.layer.borderColor = MAINCOLOR.CGColor;
        btn.layer.borderWidth = 1;
        btn.layer.cornerRadius = 3;
    }
    self.scrollView.frame = CGRectMake(6, 0, 285-12, 40);//scrollView占满自己
    self.scrollView.contentSize = CGSizeMake(x, 40);
    [self addSubview:self.scrollView];
}


-(CGSize)SizeWithString:(NSString*)str font:(UIFont *)font
{
    NSDictionary *dict = @{NSFontAttributeName:font};
    CGSize maxSize = CGSizeMake(MAXFLOAT, 34);
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}


#pragma mark - 懒加载
- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(NSMutableArray<UIButton *> *)btnMArray{
    if (_btnMArray == nil) {
        _btnMArray = [[NSMutableArray alloc]init];
    }
    return _btnMArray;
}


- (NSMutableArray<HXSelectedDataDictModel *> *)nameCodeArray{
    if (!_nameCodeArray) {
        _nameCodeArray = [[NSMutableArray alloc]init];
    }
    return _nameCodeArray;
}
@end
